//promise中断请求
// promise中断请求
function createAbortablePromise(promiseFactory, signal) {
    return new Promise((resolve, reject) => {
        // 检查是否已经中断
        if (signal && signal.aborted) {
            reject(new Error('Request aborted'));
            return;
        }

        // 监听中断信号
        const abortHandler = () => {
            reject(new Error('Request aborted'));
        };

        if (signal) {
            signal.addEventListener('abort', abortHandler);
        }

        // 执行原始Promise
        promiseFactory()
            .then(result => {
                if (signal) {
                    signal.removeEventListener('abort', abortHandler);
                }
                resolve(result);
            })
            .catch(error => {
                if (signal) {
                    signal.removeEventListener('abort', abortHandler);
                }
                reject(error);
            });
    });
}

// 使用示例:
// const controller = new AbortController();
// const signal = controller.signal;
//
// const abortablePromise = createAbortablePromise(() => {
//     return fetch('https://api.example.com/data', { signal });
// }, signal);
//
// // 中断请求
// controller.abort();


